﻿
//编辑对话框
var dlg = new MyAppDialog(700, 500);

//关闭对话框
function closeDialog(reload) {
    dlg.close();

    if (reload)
        $("#datagrid").datagrid("reload");
}

$(document).ready(function () {
    initValidate();

    $("#list-table .list-data>tr").each(function () {
        var $row = $(this);
        $row.find("[data-fieldname=Password]").rules("add", { required: true });
        $row.find("[data-fieldname=Requireusers]").rules("add", { required: true, digits: true });
    });

    //保存
    $("#btnsave").click(function (evt) {
        evt.preventDefault();
        save($(this));
    });

    //随机密码
    $("#randomPwd").click(function () {
        $(".list-datafield[data-fieldname=Password]").each(function () {
            $(this).val(_.random(100000, 999999));
        });
    });
});

//数据验证
function initValidate() {
    //表单验证
    $('#inputform').validate({
        rules: {
            EvaluateStart: {required: true },
            EvaluateEnd: { required: true },
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        messages: {}
    });
}

//保存数据
function save(obj) {
    //表单验证
    if (!$("#inputform").valid()) {
        return;
    }
    var jsondata = {
        EvalSave: {
            Id: $("#Id").val(),
            Values: {}
        },
        EvalDeptList: []
    };
    //主表数据
    $("#inputform .datafield").each(function () {
        jsondata.EvalSave.Values[$(this).attr("data-fieldname")] = $(this).val();
    });
    //科室数据
    $("#list-table .list-data>tr").each(function () {
        var $row = $(this);
        var rowdata = { Id: $row.attr("data-id") };
        $row.find(".list-datafield").each(function () {
            rowdata[$(this).attr("data-fieldname")] = $(this).val();
        });
        jsondata.EvalDeptList.push(rowdata);
    });

    //禁用保存按钮，防止重复提交
    $(obj).attr("disabled", true);

    //console.log(jsondata)

    //保存数据
    $.ajax({
        type: "post",
        url: baseurl + "Basic/EvalSetting/Save",
        data: { json: JSON.stringify(jsondata) },
        success: function (e) {
            $(obj).attr("disabled", false);
            if (e.Ret == "0") {
                $.messager.alert("消息", "保存成功！", "info");
            } else {
                $.messager.alert("错误", e.Msg, "error");
            }
        },
        error: function () {
            $(obj).attr("disabled", false);
        }
    });
}